#pragma once
#include <QAbstractListModel>
#include <QHash>
#include <QList>
#include <QByteArray>
#include <QDebug>
#include <QFile>
#include "classes.h"
#include <QTimer>

class StockListModel : public QAbstractListModel
{
    Q_OBJECT
    Q_PROPERTY(bool load READ loading WRITE setLoading NOTIFY loadChange)
public:
    enum DataType{
        ROLE_CODE = Qt::UserRole,
        ROLE_NAME,
        ROLE_DIRECTION,
        ROLE_PRICE,
        ROLE_NUMBER,
        ROLE_ID,
        ROLE_MAX
    };

public:
    explicit StockListModel(QObject *parent = nullptr);
    ~StockListModel();
    void startWork();

    virtual int rowCount(const QModelIndex &parent = QModelIndex() ) const override;
    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override;
    virtual QHash<int, QByteArray> roleNames() const override;
    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
    Q_INVOKABLE QVariant data(int row, int column, int role = Qt::DisplayRole) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    QVariant headerData(int section, Qt::Orientation orientation,
                        int role = Qt::DisplayRole) const override;

    Q_INVOKABLE void doRecordData(int code, QString name, QString direction, float price, int number);

    Q_INVOKABLE void doDeleteData(int code, int row);

    Q_INVOKABLE void doSetCurrent(int code);

    Q_INVOKABLE void doInit();

    Q_INVOKABLE void setLoading(bool load) {
        m_bLoad = load;
        emit loadChange();
    }
	
    Q_INVOKABLE const bool loading() {return m_bLoad; }

signals:
    void loadChange();
    void signalHold(const float unitPrice, const int number, const float money);
    void signalCurrentCode(const int code);
private:
    void readFile();
    void calcData(const int code);
    void saveFile();

    void initConnect();
private:
    StockInfomation m_recordData;
    bool m_bLoad = false;   /// 加载中的过程
};
